/**
 * 
 */
package com.action;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.cms.manager.CpInfoManager;
import com.cms.model.Channel;
import com.cms.model.CpInfo;
import com.cms.service.ChannelService;
import com.cms.service.ChannelServiceImpl;
import com.model.CpAccount;
import com.service.CpAccountService;
import com.service.CpAccountServiceImpl;
import com.util.Util;


public class CpAccountAction extends DefaultAction
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Logger logger;
	private CpAccountService service;
	
	public CpAccountAction()
	{
		logger = Logger.getLogger(this.getClass());
		service = new CpAccountServiceImpl();
	}
	
	public String execute()
	{
		return SUCCESS;
	}

	private List<CpAccount> cpaccounts;
	private CpAccount       cpaccount;
	private String        type;
	private List<CpInfo> cpInfos;
	private List<Channel> channels;
	private String msg;
	ChannelService chservice = new ChannelServiceImpl();
	private String cpId;
	private String channelid;
	public String list()
	{
		logger.debug("list account");
		cpaccounts = service.getCpAccounts();
		cpInfos = CpInfoManager.getInstance().getCpInfos();
		channels = chservice.getChannels();
		return SUCCESS;
	}
	
	public String show()
	{
		if (cpaccount.getId() != null && cpaccount.getId().length() > 0)
		{
			cpaccount = service.getCpAccount(cpaccount.getId());
			type = "update";
		}
		else
		{
			cpaccount.setId("");
			cpaccount.setPassword(com.utils.security.PasswordGenerator.generate(8));
			type = "insert";
		}

		channels = chservice.getChannels();
		cpInfos = CpInfoManager.getInstance().getCpInfos();
		return SUCCESS;
	}
	
	public String save()
	{
		boolean f = true;
		if(type.equals("insert"))
		{
			CpAccount ca = service.getCpAccount(cpaccount.getId());
			if(ca!=null && ca.getId().equals(cpaccount.getId()))
			{
				msg = "客户账号管理：添加失败，ID不能重复！";
				f = false;
			}
			cpaccount.setMenuvalue(cpaccount.getMenuvalue().replaceAll(" ", ""));
			cpaccount.setCreateuser(getAdminLoginName());
			cpaccount.setCreatedate(Util.dateToString(new Date()));
		}
		if(!type.equals("delete"))
		{
			if(cpId!=null)
				cpaccount.setCpId(cpId.replace(" ", ""));
			else
				cpaccount.setCpId("");
			if(channelid!=null)
				cpaccount.setChannelid(channelid.replace(" ", ""));
			else
				cpaccount.setChannelid("");
		}
		if(f)
		{
			if(service.saveCpAccount(type, cpaccount))
			{
				if(type.equals("insert"))
					msg = "客户账号管理：添加成功";
				else if(type.equals("update"))
					msg = "客户账号管理：修改成功";
				else if(type.equals("delete"))
					msg = "客户账号管理：删除成功";
			}
			else
			{
				if(type.equals("insert"))
					msg = "客户账号管理：添加失败";
				else if(type.equals("update"))
					msg = "客户账号管理：修改失败";
				else if(type.equals("delete"))
					msg = "客户账号管理：删除失败";
			}
		}
		return SUCCESS;
	}
	
	private String oldPassword;
	private String newPassword;
	public String change()
	{
		if (currentAdmin == null)
		{
			addActionError("请重新登录");
			return INPUT;
		}
		cpaccount = service.getCpAccount(cpaccount.getId());
		return SUCCESS;
	}
	
	public String password_save()
	{
		if (currentAdmin == null)
		{
			addActionError("请重新登录");
			return INPUT;
		}

		CpAccount act = service.getCpAccount(cpaccount.getId());
		act.setPassword(newPassword);
		if(service.updatePassword(cpaccount.getId(), newPassword))
		{
			msg = "客户账号管理：密码修改成功";
		}
		else
		{
			msg = "客户账号管理：密码修改失败";
		}
		return SUCCESS;
	}

	public List<CpAccount> getCpaccounts() {
		return cpaccounts;
	}

	public void setCpaccounts(List<CpAccount> cpaccounts) {
		this.cpaccounts = cpaccounts;
	}

	public CpAccount getCpaccount() {
		return cpaccount;
	}

	public void setCpaccount(CpAccount cpaccount) {
		this.cpaccount = cpaccount;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public List<CpInfo> getCpInfos() {
		return cpInfos;
	}

	public void setCpInfos(List<CpInfo> cpInfos) {
		this.cpInfos = cpInfos;
	}

	public List<Channel> getChannels() {
		return channels;
	}

	public void setChannels(List<Channel> channels) {
		this.channels = channels;
	}

	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getCpId() {
		return cpId;
	}

	public void setCpId(String cpId) {
		this.cpId = cpId;
	}

	public String getChannelid() {
		return channelid;
	}

	public void setChannelid(String channelid) {
		this.channelid = channelid;
	}
	
	
	
}
